﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using gfLibrary;
using gfPage;
using BLL;
using Model;
using IYASAKAReport;

namespace IYAKASAPortal.PrintAll
{
    public partial class PrintPage : System.Web.UI.Page
    {
        private static string printer;
        private static string s_strID;
        private static Dictionary<string, string> s_dicOutlook;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strUploadMark = "1";
                if (Request["ID"] != null && Request["ID"] != "")
                {
                    printer = Request["Printer"];
                    s_strID = Request["ID"];
                    s_dicOutlook = new Dictionary<string, string>();
                    s_dicOutlook.Add("车辆唯一性检查", "WYXRD_PD");
                    s_dicOutlook.Add("车辆特征参数检查", "CLTZCSJC_PD");
                    s_dicOutlook.Add("外观检查", "WGJC_PD");
                    s_dicOutlook.Add("安全装置检查", "AQZZJY_PD");
                    s_dicOutlook.Add("联网查询", "LWCX_PD");
                    s_dicOutlook.Add("底盘动态检查", "DTDP_PD");
                    s_dicOutlook.Add("底盘部件检查", "DPBJ_PD");
                    if (Request["UploadMark"] != null && Request["UploadMark"] != "")
                    {
                        strUploadMark = Request["UploadMark"];
                    }
                    this.Print(strUploadMark);
                }
            }
        }

        private void Print(string UploadMark)
        {
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
            var vehiclebll = bll.GetModelList("ID = " + s_strID)[0];
            string hphm = vehiclebll.HPHM;
            string vin = vehiclebll.VIN;
            string jcrq = DateTime.Parse(vehiclebll.JCRQ).ToString("yyyy-MM-dd");
            AJReport ajReport = new AJReport(vehiclebll, true);
            rpt.DsAJ ds = new rpt.DsAJ();
            Dictionary<string, Tuple<string, string, string,string,string>> dicFialItems;
            ds = (rpt.DsAJ)ajReport.BindReportResource_Report(ds, out dicFialItems);
            string strResult = "";
            List<string> lstKeys = dicFialItems.Keys.ToList();
            string strJs = "";

            strJs += " var objPrint=document.getElementById('wfPrintOcx1');";

            //清除控件上的所有打印原件
            strJs += "objPrint.wfClear();";

            //授权名和授权码
            strJs += " objPrint.wfRegName = 'guofeng'; objPrint.wfRegCode = '0EBC2502A8059BCC2Q702014';";

            //设置打印纸张
            //支持Custom,A2,A3,A3Extra,A3Trans,A4,A4Extra,A4Plus,A4Small,A4Trans A5,A5Extra,A5Trans,B4,B4ISO,B5,B5Extra,B5Trans
            strJs += "objPrint.wfPaperSize='Custom';";

            //选择打印机，参数为打印机标识名
            strJs += "objPrint.wfSelectPrinter='EPSON LQ-610KII ESC/P2';";

            //弹出选择打印机窗口
            //strJs += "objPrint.wfSelectPrinterDialog();"; 

            //设置打印方向，1为纵向、2为横向
            strJs += "objPrint.wfOrientation=1;";

            //设置打印纸宽度和高度(单位毫米)，此设置只有在纸张类型为Custom时才有效
            strJs += "objPrint.wfPaperWidth = 210;";
            strJs += "objPrint.wfPaperHeight = 140;";

            //设置打印纸的上下左右边距(单位毫米)
            strJs += "objPrint.wfPaperTopMargin = 0;";
            strJs += "objPrint.wfPaperBottomMargin = 0;";
            strJs += "objPrint.wfPaperLeftMargin = 0;";
            strJs += "objPrint.wfPaperRightMargin = 0;";

            //设置所有元件针对左边距的偏移量(单位毫米)
            strJs += "objPrint.wfOffsetLeft = 0;";

            //设置所有元件针对上边距的偏移量(单位毫米)
            strJs += "objPrint.wfOffsetTop = 20;";

            //添加一个打印元件，参数依次为：元件唯一标识，左边距(单位毫米)，上边距(单位毫米)，宽度(单位毫米)，高度(单位毫米)，字体名称，字体大小，字体眼色，字体背景颜色，字体样式，字体对齐方式
            //字体样式：0默认，1加粗，2加斜，3加下划线，4加删除线
            //字体对齐方式:0左对齐，1居中，2右对齐；注意：需要宽度为固定值
            //固定元件宽度，即文字将可以自动换行
            strJs += "objPrint.wfAddLabel('NAME', 30, 10, 150, 0, '宋体', 24, '000000', '', 1, 1);";
            strJs += "objPrint.wfAddLabel('HPHM', 30, 25, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('JCXH', 85, 25, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('VIN', 120, 25, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('VALUE', 30, 35, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('ADDRESS', 120, 110, 0, 0, '宋体', 12, '000000', '', 1, 0);";
            strJs += "objPrint.wfAddLabel('SCZJL', 120, 100, 0, 0, '宋体', 12, '000000', '', 1, 0);";


            //为组件赋值，参数为：元件唯一标识，元件要打印的文本
            strJs += "objPrint.wfAddValue('NAME', '不合格告知单');";
            strJs += "objPrint.wfAddValue('HPHM', '号牌号码：" + hphm + "');";
            strJs += "objPrint.wfAddValue('JCXH', '线号：" + Request["PrintLine"] + "');";
            strJs += "objPrint.wfAddValue('VIN', '车辆识别代号：" + vin + "');";
            strJs += "objPrint.wfAddValue('VALUE', '的车在" + jcrq + "日期的检测中存在如下不合格项目，特此告知');";

            if (UploadMark == "2")
            {
                strJs += "objPrint.wfAddValue('SCZJL','上传总结论失败');";
            }
            for (int i = 0; i < lstKeys.Count; i++)
            {

                string _strItemName = dicFialItems[lstKeys[i]].Item1;
                string _strItemResult = dicFialItems[lstKeys[i]].Item2;
                string _strItemInfo = dicFialItems[lstKeys[i]].Item3;
                
                if (i == lstKeys.Count - 1)
                {
                    if (s_dicOutlook.Keys.Contains(_strItemName))
                    {
                        strResult = i + 1 + "." + _strItemName + " " + GetResultDescribe(_strItemResult) + "不符合项： " + _strItemInfo;
                        strJs += "objPrint.wfAddLabel('BHGX" + i + "', 30 ," + (((i + 1) * 6) + 40) + " , 150, 0, '宋体', 12, '000000', '', 1,0);";
                        strJs += "objPrint.wfAddValue('BHGX" + i + "', '" + strResult + "');";
                    }
                    else
                    {
                        if (lstKeys[i].Equals("YZZDLV_PD") || lstKeys[i].Equals("YZBPHLV_PD") || lstKeys[i].Equals("EZBPHLV_PD") || lstKeys[i].Equals("EZZDLV_PD") || lstKeys[i].Equals("SZDLV_PD"))
                        {
                            strResult = i + 1 + "." + _strItemName + "： " + _strItemResult + ",左:" + dicFialItems[lstKeys[i]].Item4 + "，右：" + dicFialItems[lstKeys[i]].Item5 + ", 标准为：" + _strItemInfo;
                            strJs += "objPrint.wfAddLabel('BHGX" + i + "', 30 ," + (((i + 1) * 6) + 40) + " , 150, 0, '宋体', 12, '000000', '', 1,0);";
                            strJs += "objPrint.wfAddValue('BHGX" + i + "', '" + strResult + "');";
                        }
                        else
                        {
                            strResult = i + 1 + "." + _strItemName + "： " + _strItemResult + ", 标准为：" + _strItemInfo;
                            strJs += "objPrint.wfAddLabel('BHGX" + i + "', 30 ," + (((i + 1) * 6) + 40) + " , 150, 0, '宋体', 12, '000000', '', 1,0);";
                            strJs += "objPrint.wfAddValue('BHGX" + i + "', '" + strResult + "');";
                        }
                    }
                }
                else
                {
                    if (s_dicOutlook.Keys.Contains(_strItemName))
                    {
                        strResult = i + 1 + "." + _strItemName + " " + GetResultDescribe(_strItemResult) + "不符合项： " + _strItemInfo;
                        strJs += "objPrint.wfAddLabel('BHGX" + i + "', 30 ," + (((i + 1) * 6) + 40) + " , 150, 0, '宋体', 12, '000000', '', 1,0);";
                        strJs += "objPrint.wfAddValue('BHGX" + i + "', '" + strResult + "');";
                    }
                    else
                    {
                        if (lstKeys[i].Equals("YZZDLV_PD") || lstKeys[i].Equals("YZBPHLV_PD") || lstKeys[i].Equals("EZBPHLV_PD") || lstKeys[i].Equals("EZZDLV_PD")|| lstKeys[i].Equals("SZDLV_PD"))
                        {
                            strResult = i + 1 + "." + _strItemName + "： " + _strItemResult + ",左:" + dicFialItems[lstKeys[i]].Item4 + "，右：" + dicFialItems[lstKeys[i]].Item5 + ", 标准为：" + _strItemInfo + ",";
                            strJs += "objPrint.wfAddLabel('BHGX" + i + "', 30 ," + (((i + 1) * 6) + 40) + " , 150, 0, '宋体', 12, '000000', '', 1,0);";
                            strJs += "objPrint.wfAddValue('BHGX" + i + "', '" + strResult + "');";
                        }
                        else
                        {
                            strResult = i + 1 + "." + _strItemName + "： " + _strItemResult + ", 标准为：" + _strItemInfo;
                            strJs += "objPrint.wfAddLabel('BHGX" + i + "', 30 ," + (((i + 1) * 6) + 40) + " , 150, 0, '宋体', 12, '000000', '', 1,0);";
                            strJs += "objPrint.wfAddValue('BHGX" + i + "', '" + strResult + "');";
                        }
                    }

                }
            }

            INSPECTION_STATION_INFO_BLL _bll_station_info = new INSPECTION_STATION_INFO_BLL();
            INSPECTION_STATION_INFO station_info = _bll_station_info.GetModelList()[0];
            strJs += "objPrint.wfAddValue('ADDRESS', '" + station_info.STATION_NAME + "');";


            //立即打印到打印机
            strJs += "objPrint.wfPrint();";

            //打印预览
            //strJs += "objPrint.wfPreview();";
            gfLibrary.gfAjaxJs.RunFunction(this.UpdatePanel1, strJs);
        }


        private bool UploadResult(int id)
        {
            try
            {
                string strEmployee = string.Empty;
                string strJCLSH = string.Empty;
                Services.UploadNetInfos uploadNetInfos = new Services.UploadNetInfos();
                RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
                var info = bll.GetModelList("ID = '" + id.ToString() + "'");
                if (info.Length > 0)
                {
                    strJCLSH = info[0].JCLSH;
                }

                EMPLOYEE_USER_BLL employeeBll = new EMPLOYEE_USER_BLL();
                var employeeInfo = employeeBll.GetModelList("LICENSE_NAME LIKE '%08,%'");
                if (employeeInfo.Length > 0)
                {
                    strEmployee = employeeInfo[0].EMPLOYEE_NAME;
                }

                return uploadNetInfos.UploadNetInformation(strJCLSH, strEmployee);
            }
            catch (Exception ex)
            {

            }
            return false;

        }
        private string GetResultDescribe(string strResult)
        {
            string _describe = "";
            if (!string.IsNullOrEmpty(strResult.Trim()))
            {
                switch (strResult.Trim())
                {
                    case "1": _describe = "合格"; break;
                    case "2": _describe = "不合格"; break;
                    case "0": _describe = "未检"; break;
                    case "3": _describe = "不判定"; break;
                    case "-": _describe = "-"; break;
                }
            }
            else
            {
                _describe = "-";
            }
            return _describe;
        }
    }
}